package com.book.a20210523;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * Created by h on 2017/7/20.
 * <p>
 * 一个带有delete按钮的editview
 */

public class DeleteEditView extends FrameLayout {
    TextView title;
    EditText edit;
    ImageView imageView;
    public DeleteEditView(Context context) {
        super(context);
        init(context,null);
    }

    public DeleteEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    private void init(Context context,AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.del_editview,this);
        title = findViewById(R.id.text);
        edit = findViewById(R.id.editText);
        edit.setSaveEnabled(false);  //禁止保存状态
        imageView = findViewById(R.id.icon);
        imageView.setVisibility(View.GONE);
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                edit.setText("");
            }
        });

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DeleteEditView);
        String text = ta.getString(R.styleable.DeleteEditView_mtext);
        ta.recycle();
        setTitle(text);


        this.edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                if(!TextUtils.isEmpty(s)){
                    imageView.setVisibility(View.VISIBLE);
                }else{
                    imageView.setVisibility(View.GONE);
                }
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }
            @Override
            public void afterTextChanged(Editable s) {
            }


        });
    }
    public String getText(){
        return   edit.getText().toString();
    }
    public void setTitle(String s){
        title.setText(s);
    }
    public void setText(String s){
        edit.setText(s);
    }


    @Nullable
    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putCharSequence("nr",getText());
        bundle.putParcelable("zt",super.onSaveInstanceState());
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if(state instanceof Bundle){
            Bundle bundle=(Bundle)state;
            setText(bundle.getString("nr",""));
            super.onRestoreInstanceState(bundle.getParcelable("zt"));
            return;
        }
        super.onRestoreInstanceState(state);
    }
}
